package com.applicationLayer.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.applicationLayer.formbean.CommentForm;
import com.applicationLayer.formbean.CovoitureurForm;
import com.applicationLayer.sessionbean.SessionBean;
import com.common.domainService.ICommentService;
import com.opensymphony.xwork2.ActionSupport;

public class CommentAction extends ActionSupport {

	private static final long serialVersionUID = -4836851420666307289L;

	Logger logger = Logger.getLogger(CommentAction.class);
	
	private ICommentService commentDomainService;
	private CommentForm comment;
	private List<CommentForm> commentList;
	private List<String> satisfactionList;

	public String initComment(){
		satisfactionList = new ArrayList<String>(){
			private static final long serialVersionUID = -1096961232466601972L;
			{
				add(getText("application.comment.level0"));
				add(getText("application.comment.level1"));
				add(getText("application.comment.level2"));
				add(getText("application.comment.level3"));
				add(getText("application.comment.level4"));
				add(getText("application.comment.level5"));
			}
		};
		SessionBean.setInSession("satisfactionList", satisfactionList);
		commentList =  commentDomainService.getApplicationComments();
		SessionBean.setInSession("commentList", commentList);
		logger.info("initComment() done");
		return "SUCCESS";
	}
	
	public String postComment(){
		if(!SessionBean.checkUserInSession()){
			return "SUCCESS";
		}
		satisfactionList = (List<String>) SessionBean.getFromSession("satisfactionList");
		commentList = (List<CommentForm>) SessionBean.getFromSession("commentList");
		CovoitureurForm co = (CovoitureurForm) SessionBean.getFromSession("loggedUser");
		if(!validateField())
			return "COMMENT";
		commentDomainService.addApplicationComment(comment,co.getId_covoitureur());
		//SessionBean.UnsetFromSession("satisfactionList");
		logger.info("postComment() done");
		return "SUCCESS";
	}
	
	public boolean validateField(){
		boolean isValid=true;
		if(comment!=null){
			if (comment.getComment().length()==0){	
				addFieldError( "comment.comment", " Content is required" );
				isValid=false;
			}
			if (comment.getTitle().length()==0){	
				addFieldError( "comment.title", " Title is required" );
				isValid=false;
			}
		}
		logger.info("validate() done");
		return isValid;
	}

	
	/** GETTER **/
	
	public List<CommentForm> getCommentList() {
		return commentList;
	}
	
	public ICommentService getCommentDomainService() {
		return commentDomainService;
	}
	
	public CommentForm getComment() {
		return comment;
	}
	
	public List<String> getSatisfactionList() {
		return satisfactionList;
	}

	/** SETTER **/

	public void setCommentList(List<CommentForm> commentList) {
		this.commentList = commentList;
	}
	
	public void setCommentDomainService(ICommentService commentDomainService) {
		this.commentDomainService = commentDomainService;
	}
	
	public void setComment(CommentForm comment) {
		this.comment = comment;
	}
	
	public void setSatisfactionList(List<String> satisfactionList) {
		this.satisfactionList = satisfactionList;
	}

}